package com.nsu.cl.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nsu.cl.pojo.Grade;
import com.nsu.cl.service.GradeService;
import com.nsu.cl.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "年级控制器")
@RestController
@RequestMapping("/gradeController")
public class GradeController {

    @Autowired
    private GradeService gradeService;

    @ApiOperation("获取全部年级")
    @GetMapping("/getGradeList")
    public Result getGradeList() {
        List<Grade> grades = gradeService.getGrades();
        return Result.ok(grades);
    }

    @ApiOperation("根据年级ID获取该ID年级信息")
    @GetMapping("/getGradeById/{id}")
    public Result getGradeById(@ApiParam("年级ID") @PathVariable("id") Long id) {
        Grade grade = gradeService.getGradeById(id);
        return Result.ok(grade);
    }

    @ApiOperation("根据年级名称获取该年级信息")
    @GetMapping("/getGradeByName")
    public Result getGradeById(@ApiParam("年级ID信息") Grade grade) {
        Grade grade1 = gradeService.getGradeByName(grade.getName());
        return Result.ok(grade1);
    }

    @ApiOperation("删除Grade信息")
    @DeleteMapping("/deleteGrade")
    public Result deleteGrade(@ApiParam("要删除的所有的grade的id的JSON集合") @RequestBody List<Integer> ids) {
        // 调用服务层
        gradeService.removeByIds(ids);

        return Result.ok();
    }

    @ApiOperation("新增或修改grade，有id属性则是修改，没有则是增加")
    @PostMapping("/saveOrUpdateGrade")
    public Result saveOrUpdateGrade(@ApiParam("JSON的Grade对象") @RequestBody Grade grade) {
        // 接收参数
        // 调用服务层方法完成增加或者修改
        gradeService.saveOrUpdate(grade);
        return Result.ok();
    }

    @ApiOperation("查询根据年级名称模糊查询，带分页")
    @GetMapping("/getGrades/{pageNo}/{pageSize}")
    public Result getGrades(@ApiParam("分页查询页码数") @PathVariable("pageNo") Integer pageNo,
                            @ApiParam("分页查询页大小") @PathVariable("pageSize") Integer pageSize,
                            @ApiParam("分页查询模糊查询的名称") String name) {
        // 分页 带条件查询
        Page<Grade> page = new Page<>(pageNo, pageSize);
        // 通过服务层
        IPage<Grade> iPage = gradeService.getGradeByOpr(page, name);

        // 封装Result对象并返回
        return Result.ok(iPage);
    }
}
